package com.kb.order.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.kb.common.AlipayConfig;
import com.kb.order.pojo.CouponAccept;
import com.kb.order.pojo.Order;
import com.kb.order.pojo.OrderFood;
import com.kb.order.pojo.UserAddress;
import com.kb.order.service.ICouponAcceptService;
import com.kb.order.service.IOrderFoodService;
import com.kb.order.service.IOrderService;
import com.kb.order.service.IUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static com.kb.common.AlipayConfig.CHARSET;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    IOrderService orderService;
    @Autowired
    IUserAddressService addressService;
    @Autowired
    ICouponAcceptService acceptService;
    @Autowired
    IOrderFoodService orderFoodService;

    //刷新优惠劵
    @RequestMapping("/conpon")
    public List<CouponAccept> conpon(){
        List<CouponAccept> acceptList = acceptService.select(new CouponAccept());
        return acceptList;
    }

    //刷新地址表
    @RequestMapping("address")
    public List<UserAddress> address(){
        List<UserAddress> userAddresses = addressService.select(new UserAddress());
        return userAddresses;
    }

    //查看订单列表
    @RequestMapping("/lookOrder")
    public List<Order> lookOrder(Integer id){
        Order order = new Order();
        order.setUserId(new Long(1));
        List<Order> orderList = orderService.select(order);
        return orderList;
    }
    //查看单个订单详情 订单状态 支付状态
    @RequestMapping("/lookOrderOne")
    public List<OrderFood> lookOrderOne(String orderId){
        System.out.println("开始查询orderFood");
        OrderFood order = new OrderFood();
        order.setOrderId(orderId);
        List<OrderFood> orderFood = orderFoodService.select(order);
        System.out.println(orderFood);
        return orderFood;
    }
    //订单支付
    @RequestMapping("/payOrder")
    public void  payOrder(String orderId, Integer total, HttpServletResponse response) throws IOException {
        System.out.println("开始支付："+orderId+"--"+total);
        //&#x521d;&#x59cb;&#x5316;alipayclient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl,AlipayConfig.APP_ID,AlipayConfig.APP_PRIVATE_KEY,"json", CHARSET,AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
        //订单号
        String out_trade_no = new String(orderId);
        //付款金额
        String total_amount = new String(total.toString());
        //订单名称
        String subject = new String(orderId);

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                        + "\"total_amount\":\""+ total_amount +"\","
                        + "\"subject\":\""+ subject +"\","
                        + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String form = "";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody();
        }catch (AlipayApiException e){
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=" + CHARSET);
        response.getWriter().write(form);//直接将完整的表单html输出到页面
        response.getWriter().flush();
        response.getWriter().close();
    }
    //取消订单
    @RequestMapping("/cleOrder")
    public String cleOrder(String orderId){
        try {
            orderService.celOrder(orderId);
            return "1";
        }catch (Exception e){
            return "0";
        }
    }
    //订单退款
    @RequestMapping("/refundOrder")
    public String refundOrder(){
        return null;
    }
    //pay返回
    @RequestMapping("/toOrderList")
    public String toOrderList(String out_trade_no){
//&outTradeNo=201911046363&pid=2088102179997955&type=1
        System.out.println("订单支付返回的订单号："+out_trade_no);
        //修改数据库
        Order order = new Order();
        order.setOrderId(out_trade_no);
        order.setStatus(new Long(0));
        orderService.update(order);
        return "user_orderlist";
    }
}
